<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>functionmock.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_functionmock.js.html">functionmock.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2008 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Enable mocking of functions not attached to objects
<a name="line17"></a> * whether they be global / top-level or anonymous methods / closures.
<a name="line18"></a> *
<a name="line19"></a> * See the unit tests for usage.
<a name="line20"></a> *
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.testing&#39;);
<a name="line24"></a>goog.provide(&#39;goog.testing.FunctionMock&#39;);
<a name="line25"></a>goog.provide(&#39;goog.testing.GlobalFunctionMock&#39;);
<a name="line26"></a>goog.provide(&#39;goog.testing.MethodMock&#39;);
<a name="line27"></a>
<a name="line28"></a>goog.require(&#39;goog.object&#39;);
<a name="line29"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line30"></a>goog.require(&#39;goog.testing.StrictMock&#39;);
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>/**
<a name="line35"></a> * Class used to mock a function. Useful for mocking closures and anonymous
<a name="line36"></a> * callbacks etc. Creates a function object that extends goog.testing.StrictMock
<a name="line37"></a> * @param {string} opt_functionName the optional name of the function to mock
<a name="line38"></a> *     set to &#39;[anonymous mocked function]&#39; if not passed in.
<a name="line39"></a> * @extends {goog.testing.StrictMock}
<a name="line40"></a> * @constructor
<a name="line41"></a> */
<a name="line42"></a>goog.testing.FunctionMock = function(opt_functionName) {
<a name="line43"></a>  var fn = function() {
<a name="line44"></a>    var args = Array.prototype.slice.call(arguments);
<a name="line45"></a>    args.splice(0, 0, opt_functionName || &#39;[anonymous mocked function]&#39;);
<a name="line46"></a>    return fn.$mockMethod.apply(fn, args);
<a name="line47"></a>  };
<a name="line48"></a>  goog.object.extend(fn, new goog.testing.StrictMock({}));
<a name="line49"></a>
<a name="line50"></a>  return fn;
<a name="line51"></a>};
<a name="line52"></a>
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * Mocks an existing function. Creates a goog.testing.FunctionMock
<a name="line57"></a> * and registers it in the given scope with the name specified by functionName.
<a name="line58"></a> * @param {Object} scope The scope of the method to be mocked out.
<a name="line59"></a> * @param {string} functionName the name of the function we&#39;re going to mock.
<a name="line60"></a> * @extends {goog.testing.FunctionMock}
<a name="line61"></a> * @constructor
<a name="line62"></a> */
<a name="line63"></a>goog.testing.MethodMock = function(scope, functionName) {
<a name="line64"></a>  if (!(functionName in scope)) {
<a name="line65"></a>    throw new Error(functionName + &#39; is not a property of the given scope.&#39;);
<a name="line66"></a>  }
<a name="line67"></a>
<a name="line68"></a>  var fn = new goog.testing.FunctionMock(functionName);
<a name="line69"></a>
<a name="line70"></a>  fn.$propertyReplacer_ = new goog.testing.PropertyReplacer();
<a name="line71"></a>  fn.$propertyReplacer_.set(scope, functionName, fn);
<a name="line72"></a>  fn.$tearDown = this.$tearDown;
<a name="line73"></a>
<a name="line74"></a>  return fn;
<a name="line75"></a>};
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/**
<a name="line79"></a> * Reset the global function that we mocked back to it&#39;s original state
<a name="line80"></a> */
<a name="line81"></a>goog.testing.MethodMock.prototype.$tearDown = function() {
<a name="line82"></a>  this.$propertyReplacer_.reset();
<a name="line83"></a>};
<a name="line84"></a>
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * Mocks a global / top-level function. Creates a goog.testing.MethodMock
<a name="line89"></a> * in the global scope with the name specified by functionName.
<a name="line90"></a> * @param {string} functionName the name of the function we&#39;re going to mock.
<a name="line91"></a> * @extends {goog.testing.MethodMock}
<a name="line92"></a> * @constructor
<a name="line93"></a> */
<a name="line94"></a>goog.testing.GlobalFunctionMock = function(functionName) {
<a name="line95"></a>  return new goog.testing.MethodMock(goog.global, functionName);
<a name="line96"></a>};
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * Mocks a function. Convenience method for new goog.testing.FunctionMock
<a name="line101"></a> * @param {string} opt_functionName the optional name of the function to mock
<a name="line102"></a> *     set to &#39;[anonymous mocked function]&#39; if not passed in.
<a name="line103"></a> * @return {goog.testing.FunctionMock} the mocked function.
<a name="line104"></a> */
<a name="line105"></a>goog.testing.createFunctionMock = function(opt_functionName) {
<a name="line106"></a>  return new goog.testing.FunctionMock(opt_functionName);
<a name="line107"></a>};
<a name="line108"></a>
<a name="line109"></a>
<a name="line110"></a>/**
<a name="line111"></a> * Convenience method for creating a mocks for a method.
<a name="line112"></a> * @param {Object} scope The scope of the method to be mocked out.
<a name="line113"></a> * @param {string} functionName the name of the function we&#39;re going to mock.
<a name="line114"></a> * @return {goog.testing.MethodMock} the mocked global function.
<a name="line115"></a> */
<a name="line116"></a>goog.testing.createMethodMock = function(scope, functionName) {
<a name="line117"></a>  return new goog.testing.MethodMock(scope, functionName);
<a name="line118"></a>};
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * Convenience method for creating a mocks for a global / top-level function.
<a name="line123"></a> * @param {string} functionName the name of the function we&#39;re going to mock.
<a name="line124"></a> * @return {goog.testing.GlobalFunctionMock} the mocked global function.
<a name="line125"></a> */
<a name="line126"></a>goog.testing.createGlobalFunctionMock = function(functionName) {
<a name="line127"></a>  return new goog.testing.GlobalFunctionMock(functionName);
<a name="line128"></a>};
</pre>


</body>
</html>
